﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DesktopLauncher
{
    class TextBoxExtended : TextBox
    {
        [DefaultValue("")]
        [Description("Text which will be shown if no text entered"), Category("Appearance")]
        public string PlaceHolder { get; set; }

        /// <summary>
        /// This is much simplier than overriding Paint method
        /// </summary>
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 15: // this is the WM_PAINT message  
                    // invalidate the TextBox so that it gets refreshed properly  
                    this.Invalidate();
                    // call the default win32 Paint method for the TextBox first  
                    base.WndProc(ref m);
                    // now use our code to draw extra stuff over the TextBox  
                    this.CustomPaint();
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            }
        }

        /// <summary>
        /// Draw placeholder text (text which will be shown if no text entered)
        /// </summary>
        private void CustomPaint()
        {
            Graphics g = this.CreateGraphics();
            if ((!String.IsNullOrEmpty(PlaceHolder)) && (this.Text.Length == 0))
                g.DrawString(PlaceHolder, Font, Brushes.Gray, 0, 0);
        }
    }
}
